/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots;

import sagroup.dracots.SystemTupleType;
import sagroup.dracots.TupleType;
import lights.adapters.Tuple;
import lights.interfaces.ITuple;
import lime2.LimeTupleSpace;
import lime2.Location;

/**
 * This class encapsulates an arbitrary flat tuple with replication support
 * @author Xing Jiankuan
 *
 */
//@SuppressWarnings("serial")
public class FlatShareTuple extends ShareTuple {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8826377874145299639L;
	ITuple t;
	
	public FlatShareTuple() {
		//empty constructor
	}
	
	//construct a new master tuple
	public FlatShareTuple(ITuple t) {
		this(SystemTupleType.ANYTYPE, t);
	}
	
	//construct a new master tuple
	public FlatShareTuple(TupleType type, ITuple t) {
		super(type, DracoTS.nextVID(),
				DracoConstants.INITIAL_VER_NUM, Location.INVALID, 
				Location.INVALID, false);
		this.t = t;
	}
	
	//construct a new master tuple
	public FlatShareTuple(TupleType type, Location dest, ITuple t) {
		super(LimeTupleSpace.nextTID(), Location.HERE, dest, type, 
				DracoTS.nextVID(), DracoConstants.INITIAL_VER_NUM,
				Location.INVALID, Location.INVALID, false);
		this.t = t;
	}
	
	public static FlatShareTuple createTemplate(TupleType tupleType, ITuple t, Boolean isReplica) {
		FlatShareTuple ret = new FlatShareTuple(tupleType, t);
		ret.limeWrapper.id = null;
		ret.limeWrapper.cur = Location.HERE;
		ret.limeWrapper.dest = Location.HERE;
		ret.vid = null;
		ret.ver = null;
		ret.masterCurr = Location.UNSPECIFIED;
		ret.masterDest = Location.UNSPECIFIED;
		ret.isReplica = isReplica;
		return ret;
	}
	
	/**
	 * @return the t
	 */
	public ITuple getTuple() {
		return t;
	}

	/**
	 * @param t the t to set
	 */
	public void setTuple(ITuple t) {
		this.t = t;
	}

	/**
	 * Create a tuple that match any flat share tuple
	 * @return
	 * @author Xing Jiankuan
	 */
	public static FlatShareTuple createMatchAllTemplate() {
		return (FlatShareTuple) createMatchAllTemplateByClass(FlatShareTuple.class);
	}
	
	@Override
	protected void customCreateFromFlat(ITuple flat) {
		int start = getAllClzFieldsForTuple(this.getClass()).size() + DracoConstants.DRACO_TAGS_NUMBER;
		int end = flat.length();
		if(isMatchAll) end--;
		ITuple t = new Tuple();
		for(int i = start; i < end; i++) {
			t.insertAt(flat.get(i), i - start);
		}
		this.t = t;
	}
	
	@Override
	protected void customFlatten(ITuple flat) {
		if(t != null) {
			int start = flat.length();
			for(int i = 0; i < t.length(); i++) {
				flat.insertAt(t.get(i), i + start);
			}
		}
	}
}
